#$ARGV[0] is the 0/1 log file.
#$ARGV[1] is the length Of Series.

$file = $ARGV[0];
$lengthOfSeries = 1000;
if($ARGV[1]!=0){
	$lengthOfSeries = $ARGV[1];
}
$totalNum = $lengthOfSeries;

@pathname = split(/\\/,$file);
$len = @pathname;
$path;
print "$len\n";
for($i=$len-2;$i>=0;$i--){
	$path = "$pathname[$i]\\"."$path";
	print "$path\n";
}

$numOfSeries = 16;
$channelNum = 16;
@occRatio;
@totalIdle;
@totalBusy;

$m=0;
#get samples
$alreadyNum = 0;
open fin, "<$file" or die "can not open file $file!\n";
GETSAMPLE:{
	while (<fin>) {
		chomp;
		@info = split;

		$length = @info;
		if(($length-1)!=$channelNum){
			last GETSAMPLE;
		}
		
		for($i=0;$i<$channelNum+1;$i++){
			if($i!=0){
				if($info[$i]==1){
					$totalBusy[$i]++;
				}
				else{
					$totalIdle[$i]++;
				}
			}
		}
		$alreadyNum++;
		if($alreadyNum==$totalNum){
			last GETSAMPLE;
		}
	}
}
close fin;

#calculate the occupied ratio
for($i=0;$i<$channelNum;$i++){
	if(($totalBusy[$i+1]+$totalIdle[$i+1])==0){
		print "occRatio$i: $totalBusy[$i+1] $totalIdle[$i+1] $occRatio[$i]\n";
	}
	$occRatio[$i] = $totalBusy[$i+1]/($totalBusy[$i+1]+$totalIdle[$i+1]);
	print "occRatio$i: $totalBusy[$i+1] $totalIdle[$i+1] $occRatio[$i]\n";
}

@sample;
for($i=0;$i<$lengthOfSeries;$i++){
	for($j=0;$j<$numOfSeries;$j++){
		$sample[$j][$i]=0;
	}
}

for($j=0;$j<$numOfSeries;$j++){
	for($i=0;$i<$lengthOfSeries;$i++){
		$temprand=rand();
		#print "$temprand\n";
		if($temprand<=$occRatio[$j]){
			$sample[$j][$i]=1;
		}
		else{
			$sample[$j][$i]=0;
		}
	}
}

$fileName="random01Series.dat";
open fout, ">$path\\$fileName" or die "can not open file $path\\$fileName\n";
for($i=0;$i<$lengthOfSeries+1;$i++){
	for($j=0;$j<$numOfSeries;$j++){
		print fout "$sample[$j][$i] ";
	}
	print fout "\n";
}
close fout;

